<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Think\Page;

/**
 * 文章控制器
 * @author jry <598821125@qq.com>
 */
class InvitationController extends AdminController
{
    /**
     * 默认方法
     * @author jry <598821125@qq.com>
     */
    private $extra_html = <<<EOF
    <script type="text/javascript">
        var i=100;
        function addtime(){
            $('.item_atime:last').after('<div class="form-group item_atime " id="adddiv_'+i+'"><label class="left control-label">活动日期：</label><div class="right"><div class="input-group"><div class="input-group-addon"><i class="fa fa-calendar"></i></div><input type="text" id="_time_'+i+'" class="form-control input time" name="addtime['+i+']" value=""><a href="javascript:;" onclick="removetime($(this).prev().attr(\'id\'))">移除该日期</a></div></div></div>');        
            $('#_time_'+i).datetimepicker({
                        format      : 'yyyy-mm-dd hh:ii:00',
                        autoclose   : true,
                        todayBtn    : 'linked',
                        language    : 'zh-CN',
                        fontAwesome : true
                    });
            $('#_time_'+i).val($(':text[name="atime"]').val());
            i=i+1;
        }
        function removetime(id) {
            $('#'+id).parents('.form-group').remove();
        }
        $(function(){
            $(':text[name^="addtime"]').after('<a href="javascript:;" onclick="removetime($(this).prev().attr(\'id\'))">移除该时间</a>');
            $(':text[name="atime"]').after('<a href="javascript:;" onclick="addtime()">新增活动时间</a>');
        });
    </script>
EOF;

    public function index()
    {
        $uid = session('user_auth.uid');
        $user_group   = D('Admin/Access')->getFieldByUid($uid, 'group'); 
        // 搜索
        $keyword         = I('keyword', '', 'string');
        $condition       = array('like', '%' . $keyword . '%');
        $map['id|title'] = array($condition, $condition, '_multi' => true);

        $p             = !empty($_GET["p"]) ? $_GET['p'] : 1;
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        
        $post_object   = D('Admin/Invitation');
        $data_list     = $post_object
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('id desc')
            ->select();
        $page = new Page(
            $post_object->where($map)->count(),
            C('ADMIN_PAGE_ROWS')
        );
        
        // 使用Builder快速建立列表页面。
        $attr['name']  = '往期活动';
        $attr['title'] = '往期活动';
        $attr['class'] = 'label label-primary';
        $attr['href']  = U('Admin/Previous/index', array('iid' => '__data_id__'));

        $builder = new \Common\Builder\ListBuilder();
        
            $builder->addTopButton('addnew', array('href' => U('add'))) // 添加新增按钮
            ->setSearch('请输入ID/标题', U('index'))
            ->addRightButton('edit') // 添加编辑按钮
            ->addRightButton('forbid')
            ->addRightButton('self',$attr);
        //$('.item_atime').last().after($('.item_atime').clone(true));
            //$('.item_atime').after($('.item_atime').clone(true));
            //$('.item_atime:last').after($('.item_atime:last').clone(true));

        $builder->setMetaTitle('邀请函列表') // 设置页面标题
            ->addTableColumn('id', 'ID')
            //->addTableColumn('postname', '活动名')
            ->addTableColumn('title', '标题')
            //->addTableColumn('brand_name', '品牌名')
            ->addTableColumn('showaddress', '地址')
            //->addTableColumn('intro', '体验场简介')
            //->addTableColumn('content', '体验内容')
            ->addTableColumn('create_time', '创建时间', 'time')
            ->addTableColumn('update_time', '更新时间', 'time')
            ->addTableColumn('status', '状态', 'status')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list) // 数据列表
            ->setTableDataPage($page->show()) // 数据列表分页
            
            //->addRightButton('delete') // 添加删除按钮
            ->display();
    }

    public function previous() {
        
    }

    /**
     * 新增文档
     * @author jry <598821125@qq.com>
     */
    public function add()
    {
        if (IS_POST) {
            $post_object = D('Admin/Invitation');
            $data        = $post_object->create(format_data());
            
            if (count($_POST['addtime'])>0)
            {
                foreach ($_POST['addtime'] as &$val) {
                    if ($val) {
                        $val = strtotime($val);
                    }
                }
                $data['atime'] = $data['atime'].','.join(',',$_POST['addtime']);
            }

            if ($data) {
                $id = $post_object->add($data);
                if ($id) {
                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            $order_object = D('Admin/Invitation');
            $map['status'] = array('egt', '0'); // 禁用和正常状态
            
            

            $kol_object = D('Admin/Kol');

            $data_list     = $kol_object
            ->order('sort asc,id asc')
            ->select();

            foreach ($data_list as $key => $val) {
                $kol_list[$val['id']] = $val['name'];
            }

             // 取到的直播数据
                $liveroom_object = D('Admin/Liveroom');

                $data_list     = $liveroom_object
                // ->order('id asc')
                ->select();

                foreach ($data_list as $key => $val) {
                    $liveroom_list[$val['url']] = $val['name'];
                }

                 // 取到签到的数据
                $checkin_object = D('Admin/Checkin');

                $data_list     = $checkin_object
                // ->order('id asc')
                ->select();

                foreach ($data_list as $key => $val) {
                    $checkin_list[$val['url']] = $val['name'];
                }


            
               
           

            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增邀请函') // 设置页面标题
                ->setPostUrl(U('add')) // 设置表单提交地址
                ->addFormItem('brand_id','checkbox','KOL绑定','KOL绑定',$kol_list)
                ->addFormItem('cover','subpicture','封面','封面','305/158')
                ->addFormItem('title','text','标题','标题')
                ->addFormItem('atime','datetime','活动日期','活动日期')
                ->addFormItem('showaddress','text','前台显示地址','前台显示地址')
                ->addFormItem('address','text','具体地址','具体地址')
                //->addFormItem('intro','text','体验场简介','体验场简介')
                //->addFormItem('content','text','体验内容','体验内容')
                ->addFormItem('num','text','课堂席位','课堂席位')
                ->addFormItem('price','text','课堂定价','课堂定价')
                ->addFormItem('length','text','课堂时长','课堂时长')
                ->addFormItem('people','text','适合人群','适合人群')
                /*
                ->addFormItem('s1_num','text','一类席位','一类席位')
                ->addFormItem('s1_price','text','一类定价','一类定价')
                ->addFormItem('s1_name','text','显示名称','显示名称')
                ->addFormItem('s2_num','text','二类席位','二类席位')
                ->addFormItem('s2_price','text','二类定价','二类定价')
                ->addFormItem('s2_name','text','显示名称','显示名称')
                ->addFormItem('s3_num','text','三类席位','三类席位')
                ->addFormItem('s3_price','text','三类定价','三类定价')
                ->addFormItem('s3_name','text','显示名称','显示名称')
                ->addFormItem('s4_num','text','四类席位','四类席位')
                ->addFormItem('s4_price','text','四类定价','四类定价')
                ->addFormItem('s4_name','text','显示名称','显示名称')
                */
                ->addFormItem('envi_img','pictures','环境图片','环境图片')
                ->addFormItem('tools','textarea','工具','工具')
                ->addFormItem('tools_img','pictures','工具图片','工具图片')
                ->addFormItem('step','textarea','课程步骤','课程步骤')
                ->addFormItem('step_img','pictures','课程步骤图片','课程步骤图片')
                ->addFormItem('result_img','pictures','成品效果图片','成品效果图片')
				->addFormItem('videourl','text','腾讯视频地址','https://v.qq.com/txp/iframe/player.html?vid=b0851c9h0b2')

                ->addFormItem('isprice','radio','是否收费','是否收费',array('0'=>'免费','1'=>'收费'))
                ->addFormItem('sign_url','select','签到页地址','签到页地址', $checkin_list)
                ->addFormItem('live_url','select','直播间地址','直播间地址', $liveroom_list)
                ->addFormItem('sign_img1','picture','签到表单背景图','签到表单背景图')
                ->addFormItem('sign_img2','picture','签到流程背景图','签到流程背景图')

                //->addFormItem('acontent','kindeditor','活动内容','活动内容')
                //->addFormItem('previous','picture','往期活动','往期活动')
                //->addFormItem('previous_url','text','往期链接','往期链接')
                ->setExtraHtml($this->extra_html)
                //->addFormItem('num', 'select', '场次(1000元/人次)', '场次',array('5' => 5, '10' => 10,'15'=>15,'20'=>20),'','id=num')
                ->display();
        }
    }

    /**
     * 编辑文章
     * @author jry <598821125@qq.com>
     */
    public function edit($id)
    {
        if (IS_POST) {
            $post_object = D('Admin/Invitation');
            $data        = $post_object->create(format_data());
            if (count($_POST['addtime'])>0)
            {
                foreach ($_POST['addtime'] as &$val) {
                    if ($val) {
                        $val = strtotime($val);
                    }
                }
                $data['atime'] = $data['atime'].','.join(',',$_POST['addtime']);
            }
            if ($data) {
                $id = $post_object->save($data);
                if ($id !== false) {
                    $this->success('更新成功', U('index'));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            $order_object = D('Admin/Invitation');
            $map['status'] = array('egt', '0'); // 禁用和正常状态
            
            

            $kol_object = D('Admin/Kol');

            $data_list     = $kol_object
            ->order('sort asc,id asc')
            ->select();

            foreach ($data_list as $key => $val) {
                $kol_list[$val['id']] = $val['name'];
            }

             // 取到的直播数据
                $liveroom_object = D('Admin/Liveroom');

                $data_list     = $liveroom_object
                ->order('id asc')
                ->select();

                foreach ($data_list as $key => $val) {
                    $liveroom_list[$val['url']] = $val['name'];
                }

                 // 取到签到的数据
                $checkin_object = D('Admin/Checkin');

                $data_list     = $checkin_object
                ->order('id asc')
                ->select();

                foreach ($data_list as $key => $val) {
                    $checkin_list[$val['url']] = $val['name'];
                }

            $builder = new \Common\Builder\FormBuilder();

            $invadata = D('Admin/Invitation')->find($id);
            $addtime = explode(',',$invadata['atime']);
            if (count($addtime)>1) {
                //echo "a";
                $invadata['atime'] = $addtime[0];
                array_shift($addtime);
                //unset($addtime);
            }else{
                $invadata['atime'] = $addtime[0];
                unset($addtime);
            }
            
            // 使用FormBuilder快速建立表单页面。
            $builder->setMetaTitle('编辑邀请函') // 设置页面标题
                ->setPostUrl(U('edit')) // 设置表单提交地址
                ->addFormItem('id', 'hidden', 'ID', 'ID')
                ->addFormItem('brand_id','checkbox','KOL绑定','KOL绑定',$kol_list)

                ->addFormItem('cover','subpicture','封面','封面','305/158')
                ->addFormItem('title','text','标题','标题')
                ->addFormItem('atime','datetime','活动日期','活动日期');
                foreach ($addtime as $key=>$val) {
                    $builder->addFormItem('addtime['.$key.']','datetime','活动日期','活动日期');
                    $invadata['addtime['.$key.']'] = $val;
                }
            $builder->addFormItem('showaddress','text','前台显示地址','前台显示地址')
                ->addFormItem('address','text','具体地址','具体地址')
                
                //->addFormItem('intro','text','体验场简介','体验场简介')
                //->addFormItem('content','text','体验内容','体验内容')

                ->addFormItem('num','text','课堂席位','课堂席位')
                ->addFormItem('price','text','课堂定价','课堂定价')
                ->addFormItem('length','text','课堂时长','课堂时长')
                ->addFormItem('people','text','适合人群','适合人群')
                
                /*
                ->addFormItem('s1_num','text','一类席位','一类席位')
                ->addFormItem('s1_price','text','一类定价','一类定价')
                ->addFormItem('s1_name','text','显示名称','显示名称')
                ->addFormItem('s2_num','text','二类席位','二类席位')
                ->addFormItem('s2_price','text','二类定价','二类定价')
                ->addFormItem('s2_name','text','显示名称','显示名称')
                ->addFormItem('s3_num','text','三类席位','三类席位')
                ->addFormItem('s3_price','text','三类定价','三类定价')
                ->addFormItem('s3_name','text','显示名称','显示名称')
                ->addFormItem('s4_num','text','四类席位','四类席位')
                ->addFormItem('s4_price','text','四类定价','四类定价')
                ->addFormItem('s4_name','text','显示名称','显示名称')
                */

                ->addFormItem('envi_img','pictures','环境图片','环境图片')
                ->addFormItem('tools','textarea','工具','工具')
                ->addFormItem('tools_img','pictures','工具图片','工具图片')
                ->addFormItem('step','textarea','课程步骤','课程步骤')
                ->addFormItem('step_img','pictures','课程步骤图片','课程步骤图片')
                ->addFormItem('result_img','pictures','成品效果图片','成品效果图片')
				->addFormItem('videourl','text','腾讯视频地址','https://v.qq.com/txp/iframe/player.html?vid=b0851c9h0b2')
                ->addFormItem('isprice','radio','是否收费','是否收费',array('0'=>'免费','1'=>'收费'))
                
                ->addFormItem('sign_url','select','签到页地址','签到页地址', $checkin_list)
                ->addFormItem('live_url','select','直播间地址','直播间地址', $liveroom_list)
                ->addFormItem('sign_img1','picture','签到表单背景图','签到表单背景图')
                ->addFormItem('sign_img2','picture','签到流程背景图','签到流程背景图')
                //->addFormItem('acontent','kindeditor','活动内容','活动内容')
                //->addFormItem('previous','picture','往期活动','往期活动')
                //->addFormItem('previous_url','text','往期链接','往期链接')
                ->setExtraHtml($this->extra_html)
                ->setFormData($invadata)
                //->addFormItem('num', 'select', '场次(1000元/人次)', '场次',array('5' => 5, '10' => 10,'15'=>15,'20'=>20),'','id=num')
                ->display();
        }
    }

    // 将树形列表转换为树
    public function list_as_tree($list, $extra = null, $key = 'id', $title_field = 'title')
    {
        //转换成树状列表(非严格模式)
        $tree = new \Util\Tree();
        $list = $tree->array2tree($list, $title_field, 'id', 'pid', 0, false);

        if ($extra) {
            $result[0] = $extra;
        }

        //转换成一维数组
        foreach ($list as $val) {
            $result[$val[$key]] = $val['title_show'];
        }
        return $result;
    }
}
